#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2022/10/21 10:08
# @Author  : 招财大龙猫
# @File    : make_file.py.py
# @Description : 批量生成文件
import datetime
import os
import random
import time
import faker

f_random = faker.Faker(locale='zh-CN')
file_path = '/home/pftpd/'  # 保存文件目录


def make_file(num):
    for i in range(num):
        now_time = int(datetime.datetime.now().timestamp())
        file_name = "{}_{}_{}_{}_{}_{}".format(now_time, num, i, f_random.ssn(), f_random.iban(),
                                               f_random.sha256(raw_output=False))
        with open(file_path + file_name + '.txt', "a") as f:
            file_context_1 = datetime.datetime.now()
            profile_lt = [f"{k}:{v}" for k, v in f_random.profile().items()]
            file_context_2 = '\n    '.join(profile_lt)
            file_context_3 = f_random.text()
            f.write(f"文件写入时间：{file_context_1}\n"
                    f"个人档案信息：\n    {file_context_2}\n"
                    f"个人文件内容：\n    {file_context_3}")
            print(f'生成{file_name}文件成功')


if __name__ == '__main__':
    # 如果地址不存在，则自动创建log文件夹
    if not os.path.exists(file_path):
        os.mkdir(file_path)
    while True:
        write_time = datetime.datetime.now()
        random_no = random.randint(100, 300)
        try:
            make_file(random_no)
        except Exception as exp:
            print('批量生成文件错误, 错误信息', exp)
            break
        else:
            print(f'{write_time} 生成{random_no}个文件完成')
        time.sleep(60)  # 每分钟产生文件数量
